package com.lihao.wanandroid.app.base

import android.widget.ProgressBar
import android.widget.TextView
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.lihao.jetpackcore.base.BaseViewModel
import com.lihao.jetpackcore.base.BaseVmActivity
import com.lihao.jetpackcore.ext.getAppViewModel
import com.lihao.wanandroid.R
import com.lihao.wanandroid.app.event.AppViewModel
import com.lihao.wanandroid.app.event.EventViewModel
import com.lihao.wanandroid.app.util.SettingUtil

abstract class BaseActivity<VM : BaseViewModel> : BaseVmActivity<VM>() {

    private var dialog: MaterialDialog? = null

    //Application全局的ViewModel，里面存放了一些账户信息，基本配置信息等
    val shareViewModel: AppViewModel by lazy { getAppViewModel<AppViewModel>() }

    //Application全局的ViewModel，用于发送全局通知操作
    val eventViewModel: EventViewModel by lazy { getAppViewModel<EventViewModel>() }

    /**
     * 创建liveData观察者
     */
    override fun createObserver() {}

    /**
     * 打开等待框
     */
    override fun showLoading(message: String) {
        if (dialog == null) {
            dialog = this.let {
                MaterialDialog(it)
                    .cancelable(true)
                    .cancelOnTouchOutside(false)
                    .customView(R.layout.layout_custom_progress_dialog_view)
                    .lifecycleOwner(this)
            }
            dialog?.getCustomView()?.run {
                this.findViewById<TextView>(R.id.loading_tips).text = message
                this.findViewById<ProgressBar>(R.id.progressBar).indeterminateTintList =
                    SettingUtil.getOneColorStateList(R.color.colorPrimary)
            }
        }
        dialog?.show()
    }

    /**
     * 关闭等待框
     */
    override fun dismissLoading() {
        dialog?.dismiss()
    }

}